package com.shengde.jinwawaforleader.model.myJob.monitoring;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProShowClickGoodCancle;
import com.shengde.jinwawaforleader.views.WheelView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Arrays;

/**
 * 设置开放监控时间段
 * Created by Administrator on 2016/10/25 0025.
 */
public class OpenTimeActivity extends BaseActivity {
	private static final String[] WEEKDAY = new String[]{"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
	private static final String[] TIMES = new String[]{"00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00",
			"09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00",
			"19:00", "20:00", "21:00", "22:00", "23:00", "24:00"};
	private int startWeek;
	private int endWeek;
	private int startTime;
	private int endTime;
	private static final String TAG = "OpenTimeActivity";
	private WheelView weekday_begin;
	private WheelView time_begin;
	private WheelView weekday_end;
	private WheelView time_end;
	private TextView cancel;
	private TextView sure;
	private TextView tv_title;

	private String deviceSerial;
	private int start_time;
	private int start_week;
	private int end_time;
	private int end_week;

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.mydialog_class_details);
	}

	@Override
	public void initView() {
		weekday_begin = (WheelView) findViewById(R.id.weekday_begin);
		weekday_end = (WheelView) findViewById(R.id.weekday_end);

		time_begin = (WheelView) findViewById(R.id.time_begin);
		time_end = (WheelView) findViewById(R.id.time_end);

		cancel = (TextView) findViewById(R.id.tv_back);
		sure = (TextView) findViewById(R.id.fill_add);
		tv_title = (TextView) findViewById(R.id.tv_title);
	}

	@Override
	public void initData() {
		deviceSerial = getIntent().getStringExtra("deviceSerial");
		start_time = getIntent().getIntExtra("start_time", 1);
		start_week = getIntent().getIntExtra("start_week", 1);
		end_time = getIntent().getIntExtra("end_time", 1);
		end_week = getIntent().getIntExtra("end_week", 1);
		sure.setText(getString(R.string.finsh));
		tv_title.setText(getString(R.string.setting_time));
		Log.i(TAG, "initData: start_time" + start_time);
		Log.i(TAG, "initData: start_week" + start_week);
		Log.i(TAG, "initData: end_time" + end_time);
		Log.i(TAG, "initData: end_week" + end_week);
		weekday_begin.setOffset(1);
		weekday_begin.setSeletion(start_week);
		weekday_begin.setItems(Arrays.asList(WEEKDAY));
		weekday_begin.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, String item) {
				super.onSelected(selectedIndex, item);
				Log.i(TAG, "weekday_beginselectedIndex" + selectedIndex + "item" + item);
				startWeek = selectedIndex - 1;
			}
		});
		weekday_end.setOffset(1);
		weekday_end.setSeletion(end_week);
		weekday_end.setItems(Arrays.asList(WEEKDAY));
		weekday_end.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, String item) {
				super.onSelected(selectedIndex, item);
				Log.i(TAG, "weekday_endselectedIndex" + selectedIndex + "item" + item);
				endWeek = selectedIndex - 1;

			}
		});
		time_begin.setOffset(1);
		time_begin.setSeletion(start_time);
		time_begin.setItems(Arrays.asList(TIMES));
		time_begin.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, String item) {
				super.onSelected(selectedIndex, item);
				Log.i(TAG, "time_beginselectedIndex" + selectedIndex + "item" + item);
				startTime = selectedIndex - 1;

			}
		});
		time_end.setOffset(1);
		time_end.setSeletion(end_time);
		time_end.setItems(Arrays.asList(TIMES));
		time_end.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, String item) {
				super.onSelected(selectedIndex, item);
				Log.i(TAG, "time_endselectedIndex" + selectedIndex + "item" + item);
				endTime = selectedIndex - 1;

			}
		});

	}

	@Override
	public void initListener() {
		cancel.setOnClickListener(this);
		sure.setOnClickListener(this);
	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.fill_add:
				showSaveTime(startWeek, endWeek, startTime, endTime);
				break;
			case R.id.tv_back:
				finish();
				break;
		}
	}

	/**
	 * 上传时间
	 *
	 * @param startWeek
	 * @param endWeek
	 * @param startTime
	 * @param endTime
	 */
	private void showSaveTime(int startWeek, int endWeek, int startTime, int endTime) {
		if (endWeek < startWeek) {
			Toast.makeText(this, "星期不能比之前小", Toast.LENGTH_SHORT).show();
			return;
		} else if (endTime <= startTime) {
			Toast.makeText(this, "时间不能比之前小", Toast.LENGTH_SHORT).show();

			return;
		} else {

			String url = "";
			if (User.isTest) {
				url = BaseProtocol.HOST_BASE_TEST + "/camera/checkTime";
			} else {
				url = BaseProtocol.HOST_BASE + "/camera/checkTime";
			}
			//"http://192.168.1.40:8080/api/camera/cameraLookteacher"
			//http://120.76.155.123:8080/api/camera/cameraLookteacher?deviceSerial
			Log.i(TAG, "showTeacherPermissionList:deviceSerial " + deviceSerial);
			RequestParams params  = BaseProtocol.getRequestParams(url);
			params.addParameter("deviceSerial", deviceSerial);
			params.addParameter("startWeek", startWeek);
			params.addParameter("endWeek", endWeek);
			params.addParameter("startTime", startTime);
			params.addParameter("endTime", endTime);
			params.addParameter("schoolid", User.getSchoolId(this));
			x.http().post(params, new Callback.CommonCallback<String>() {
				@Override
				public void onSuccess(String result) {
					ProShowClickGoodCancle pro_monitoring_list = new Gson().fromJson(result, ProShowClickGoodCancle.class);
					Log.i(TAG, "onSuccess: result" + result.toString());
					Log.i(TAG, "onSuccess: code" + pro_monitoring_list.getCode());
					if (pro_monitoring_list != null && pro_monitoring_list.getCode() == 0) {
						Toast.makeText(OpenTimeActivity.this, "时间设置成功", Toast.LENGTH_SHORT).show();
						finish();
					}
				}

				@Override
				public void onError(Throwable ex, boolean isOnCallback) {
					Log.i(TAG, "onError: ex" + ex);
				}

				@Override
				public void onCancelled(CancelledException cex) {

				}

				@Override
				public void onFinished() {

				}
			});
		}

	}
}
